﻿using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Metadata;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Data.Services.Common;
using System.Runtime.Serialization;

namespace Sustainalytics.DynamicsCRMGateway.Entities
{
    [DataContractAttribute()]
    [EntityLogicalNameAttribute("new_companiesgroup")]
    [DataServiceKeyAttribute("Id")]
    [EntityAttribute("new_companiesgroupSet")]
    public class CompaniesGroup : CrmEntity, INameable
    {
        public CompaniesGroup() :
            base(EntityLogicalName)
        {
        }

        public const string EntityLogicalName = "new_companiesgroup";

        public const int EntityTypeCode = 10033;

        [AttributeLogicalNameAttribute("new_companiesgroupid")]
        public Nullable<Guid> CompaniesGroupId
        {
            get { return this.GetAttributeValue<Nullable<Guid>>("new_companiesgroupid"); }
            set { this.SetPrimaryIdAttributeValue<Nullable<Guid>>("new_companiesgroupId", "new_companiesgroupid", value); }
        }

        [AttributeLogicalNameAttribute("new_companiesgroupid")]
        public override Guid Id
        {
            get { return base.Id; }
            set { this.CompaniesGroupId = value; }
        }

        [AttributeLogicalNameAttribute("new_name")]
        public string Name
        {
            get { return this.GetAttributeValue<string>("new_name"); }
            set { this.SetAttributeValue<string>("new_name", "new_name", value); }
        }


        [AttributeLogicalNameAttribute("createdon")]
        public DateTime CreatedOn
        {
            get { return this.GetAttributeValue<DateTime>("createdon"); }
            set { this.SetAttributeValue<DateTime>("createdon", "createdon", value); }
        }

        [AttributeLogicalNameAttribute("modifiedon")]
        public DateTime ModifiedOn
        {
            get { return this.GetAttributeValue<DateTime>("modifiedon"); }
            set { this.SetAttributeValue<DateTime>("modifiedon", "modifiedon", value); }
        }
        
        [AttributeLogicalNameAttribute("new_standard")]
        public Nullable<bool> IsStandard
        {
            get { return this.GetAttributeValue<Nullable<bool>>("new_standard"); }
            set { this.SetAttributeValue<Nullable<bool>>("new_Standard", "new_standard", value); }
        }

        [RelationshipSchemaNameAttribute("new_new_companiesgroup_new_companiesgroupcons")]
        public IEnumerable<CompaniesGroupConstituent> CompaniesGroupConstituents
        {
            get { return this.GetRelatedEntities<CompaniesGroupConstituent>("new_new_companiesgroup_new_companiesgroupcons", null); }
            set { this.SetRelatedEntities<CompaniesGroupConstituent>("new_new_companiesgroup_new_companiesgroupcons", "new_new_companiesgroup_new_companiesgroupcons", null, value); }
        }

        [RelationshipSchemaNameAttribute("new_new_companiesgroup_new_accountuniverse")]
        public IEnumerable<AccountUniverse> AccountUniverses
        {
            get { return this.GetRelatedEntities<AccountUniverse>("new_new_companiesgroup_new_accountuniverse", null); }
            set { this.SetRelatedEntities<AccountUniverse>("new_new_companiesgroup_new_accountuniverse", "new_new_companiesgroup_new_accountuniverse", null, value); }
        }

        /// <summary>
        /// Constructor for populating via LINQ queries given a LINQ anonymous type
        /// <param name="anonymousType">LINQ anonymous type.</param>
        /// </summary>
        public CompaniesGroup(object anonymousType) :
            this()
        {
            foreach (var p in anonymousType.GetType().GetProperties())
            {
                var value = p.GetValue(anonymousType, null);
                if (p.PropertyType == typeof(Guid))
                {
                    // Type is Guid, must be Id
                    base.Id = (Guid)value;
                    Attributes["new_companiesgroupid"] = base.Id;
                }
                else if (p.Name == "FormattedValues")
                {
                    // Add Support for FormattedValues
                    FormattedValues.AddRange((FormattedValueCollection)value);
                }
                else
                {
                    Attributes[p.Name.ToLowerInvariant()] = value;
                }
            }
        }
    }
}
